package com.ruoyi.web.controller.api;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.UtilTool;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 用户个人中心接口
 */
@Slf4j
@RestController
@RequestMapping("/api/user")
public class AppCenterApiController extends BaseController {
    @Autowired
    private IAppNoticeService appNoticeService;
    @Autowired
    private IAppPersonalCenterService appPersonalCenterService;
    @Autowired
    private IWebSiteService webSiteService;
    @Autowired
    private IAppUserNoticeService appUserNoticeService;
    @Autowired
    private IAppUserService appUserService;

    /**
     * 登录后获取个人通知应用
     */
    @PostMapping("/getPersonalCenterList")
    @ResponseBody
    public AjaxResult getPersonalCenterList(AppUserNotice appUserNotice) {
        checkNotice(new AppNotice());
        int number = getNumber(appUserNotice);
        List<AppPersonalCenter> list = appPersonalCenterService.getPersonalCenterList();
        for (AppPersonalCenter center:list){
            WebSite webSite = webSiteService.selectWebSiteById(center.getSiteId());
            if (!UtilTool.isNull(webSite)){
                center.setRemark(webSite.getWebUrl());
            }
        }
        JSONObject object = new JSONObject();
        object.put("number",number);
        object.put("list",list);
        return AjaxResult.success(object);
    }

    /**
     * 通知列表
     */
    @PostMapping("/getNoticeList")
    @ResponseBody
    public TableDataInfo getNoticeList(AppNotice appNotice, HttpServletRequest request) {
        String uuid = request.getHeader("uuid");
        String userName = request.getHeader("userName");
        AppUserNotice appUserNotice = new AppUserNotice();
        if (UtilTool.isNull(uuid)){
            appUserNotice.setUserName(userName);
        }else {
            appUserNotice.setUuid(uuid);
        }
        appNotice.setState(1);
        startPage();
        List<AppNotice> list = appNoticeService.selectAppNoticeList(appNotice);
        for (AppNotice notice:list){
            appUserNotice.setNoticeId(notice.getId());
            List<AppUserNotice> appUserNotices = appUserNoticeService.selectAppUserNoticeList(appUserNotice);
            notice.setRemark(appUserNotices.size()+"");
        }
        return getDataTable(list);
    }

    /**
     * 阅读回调删除通知记录
     * @param appUserNotice
     * @return
     */
    @PostMapping("/readNotice")
    @ResponseBody
    public AjaxResult readNotice(AppUserNotice appUserNotice){
        if (UtilTool.isNull(appUserNotice.getUserName()) && UtilTool.isNull(appUserNotice.getUuid())){
            return AjaxResult.error("用户参数不能为空");
        }
        if (UtilTool.isNull(appUserNotice.getNoticeId())){
            return AjaxResult.error("通知id不能为空");
        }
        return toAjax(appUserNoticeService.del(appUserNotice));
    }

    //添加AppUserNotice记录
    private void addAppUserNotice(long noticeId){
        List<AppUser> appUsers = appUserService.selectAppUserList(new AppUser());
        for (AppUser user:appUsers){
            AppUserNotice appUserNotice = new AppUserNotice();
            // type 0默认注册用户 1手机唯一码用户
            if (user.getType()==0){
                appUserNotice.setUserName(user.getUserName());
            }else {
                appUserNotice.setUuid(user.getUuid());
            }
            appUserNotice.setNoticeId(noticeId);
            appUserNoticeService.insertAppUserNotice(appUserNotice);
        }
    }

    /**
     * 查询是否有没有更新的通知
     * @param appNotice
     */
    private void checkNotice(AppNotice appNotice){
        appNotice.setState(0);
        List<AppNotice> list = appNoticeService.selectAppNoticeList(appNotice);
        for (AppNotice notice:list){
            if (!UtilTool.isNull(notice.getEffectTime())){
                if (notice.getEffectTime().getTime()<System.currentTimeMillis()){
                    notice.setState(1);
                    notice.setUpdateTime(new Date(System.currentTimeMillis()));
                    appNoticeService.updateAppNotice(notice);
                    addAppUserNotice(notice.getId());
                }
            }

        }
    }

    private int getNumber(AppUserNotice appUserNotice){
        List<AppUserNotice> list = appUserNoticeService.selectAppUserNoticeList(appUserNotice);
        return list.size();
    }



}
